#!/bin/bash

# 检查是否以 root 用户运行
if [ "$(id -u)" -ne 0 ]; then
    echo "此脚本必须以 root 用户运行" 1>&2
    exit 1
fi

# 停止并禁用服务
echo "停止并禁用 2RTK Caster 服务..."
systemctl stop 2rtk-caster.service
systemctl disable 2rtk-caster.service

# 删除服务文件
echo "删除 systemd 服务文件..."
rm -f /etc/systemd/system/2rtk-caster.service
systemctl daemon-reload

# 删除二进制文件
echo "删除二进制文件..."
rm -f /usr/local/sbin/caster

# 询问是否删除配置文件和日志
read -p "是否删除配置文件和日志？(y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "删除配置文件和日志..."
    rm -rf /usr/local/etc/2rtk
    rm -rf /var/log/2rtk
    echo "配置文件和日志已删除"
else
    echo "保留配置文件和日志"
fi

# 询问是否删除用户和组
read -p "是否删除 caster 用户和组？(y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "删除 caster 用户和组..."
    userdel caster
    groupdel caster
    echo "caster 用户和组已删除"
else
    echo "保留 caster 用户和组"
fi

echo "2RTK Caster 已成功卸载"